package cn.Y_Qiu.org.controller;

import cn.Y_Qiu.basic.util.Ajaxresult;
import cn.Y_Qiu.basic.util.DetailPatchUtil;
import cn.Y_Qiu.basic.util.PageList;
import cn.Y_Qiu.org.domain.DictionaryDetail;
import cn.Y_Qiu.org.query.DictionaryDetailQuery;
import cn.Y_Qiu.org.service.IDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionarydetail")
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryTypeService;

    // 添加
    @PutMapping
    public  Ajaxresult addOrUpdate(@RequestBody DictionaryDetail dictionaryType){
        try {
            if (dictionaryType.getId() == null) {
                dictionaryTypeService.add(dictionaryType);
            } else {
                dictionaryTypeService.update(dictionaryType);
            }
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage("炸了");
        }
    }

    // 删除
    @DeleteMapping("/{id}")
    public Ajaxresult delById(@PathVariable("id") Long id){
        try {
            dictionaryTypeService.remove(id);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me()
                    .setMessage("失败");
        }
    }
    // 查单
    @GetMapping("/{id}")
    public DictionaryDetail queryById(@PathVariable("id") Long id){
        return dictionaryTypeService.queryById(id);
    }
    // 查全
    @GetMapping
    public List<DictionaryDetail> queryAll(){
        return dictionaryTypeService.listAll();
    }
    @PostMapping
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery dictionarydeatailQuery){
        return dictionaryTypeService.queryPage(dictionarydeatailQuery);
    }


    @PatchMapping
    public Ajaxresult patchremove(@RequestBody DetailPatchUtil detailPatchUtil){
        System.out.println("到达controller");
        try {
            dictionaryTypeService.patchremove(detailPatchUtil);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage("失败");
        }

    }
}
